using Assets.Scripts.Common;
using Assets.Scripts.Controllers;
using TMPro;
using UnityEngine;

namespace Assets.Scripts.UI
{
    public class CurrencyIndicator : MonoBehaviour
    {
        [SerializeField] private CurrencyId _currencyId;
        [SerializeField] private TMP_Text _text;
        [SerializeField] private string _postfix;

        private void Start()
        {
            EconomyController.Instance.CurrencyBalanceChanged += HandleCurrencyBalanceChange;

            UpdateText();
        }

        private void HandleCurrencyBalanceChange(CurrencyId currencyId, int value)
        {
            if (currencyId != _currencyId) return;

            UpdateText();
        }

        private void UpdateText()
        {
            _text.text = _postfix + EconomyController.Instance.GetBalance(_currencyId).ToString("N0");
        }
    }
}
